#!/usr/bin/env python
################################################################################
#            __  __            _   ____        _ _ ____        _              #
#           |  \/  | ___  __ _| |_| __ )  __ _| | | __ )  ___ | |_           #
#           | |\/| |/ _ \/ _` | __|  _ \ / _` | | |  _ \ / _ \| __|         #
#           | |  | |  __/ (_| | |_| |_) | (_| | | | |_) | (_) | |_          #
#           |_|  |_|\___|\__,_|\__|____/ \__,_|_|_|____/ \___/ \__|          #
#                                                                             #
#                                     by                                       #
# _______ .______       _______  _______   ______   ______   _______   _______  #
#|   ____||   _  \     |   ____||   ____| /      | /  __  \ |       \ |   ____|  #
#|  |__   |  |_)  |    |  |__   |  |__   |  ,----'|  |  |  ||  .--.  ||  |__      #
#|   __|  |      /     |   __|  |   __|  |  |     |  |  |  ||  |  |  ||   __|      #
#|  |     |  |\  \----.|  |____ |  |____ |  `----.|  `--'  ||  '--'  ||  |____      #
#|__|     | _| `._____||_______||_______| \______| \______/ |_______/ |_______|    #
#                                                                                 #
#                               Connect Core                                     #
#                             Circuits FrameWork                                #
#
# Coders
#
#	Justin Giorgi (rebel_kid)
#
# Debuggers
#
#	Justin Giorgi (rebel_kid)
#	Lachlan Gilmour (chilli0)
#	David Liedle (spiderbyte)
#	(Polarina)
#	(prologic)
#
################################################################################
#                                                                              #
#                               GPL Licensed                                   #
#                                                                              #
################################################################################

# Imports
from circuits.core import listener, Event, Component

# Sets plugin variables
__id__ = "connectionmanagement"
__core__ = True

# Creates event classes
class AllQuit(Event): pass
class IRCquit(Event): pass
class IRCmessage(Event): pass
class NewConnect(Event): pass
class Reconnect(Event): pass
class AllReconnect(Event): pass

class Connections(Component):

	def __init__(self, *args, **kwargs):
		# Important: Call the super constructors to initialize the Component.
		super(Network, self).__init__(*args, **kwargs)
		self.channel = kwargs["channel"]
	
	# Quits all networks	
	@listener("command-allquit")
	def allquit(self, **kwargs):
		# Checks botadmin permissions
		if kwargs["botadmin"] >= 2:
		
			# Checks if a message is given
			if len(kwargs["command"]) > 1:
				qmessage = kwargs["command"][1:]
			else:
				qmessage = "MeatBallBot is being shutdown."
			
			# Issues allquit event	
			self.push(AllQuit(qmessage), "allquit", self.channel)
		
		# Replies not bot admin
		else:
			self.push(IRCmessage(kwargs["destination"], "You are not a bot administrator."), "issue-message", self.channel)
		
	# Quits network	
	@listener("command-quit")
	def quit(self, **kwargs):
		# Checks botadmin permissions
		if kwargs["botadmin"] >= 2:
		
			# Checks if a message is given
			if len(kwargs["command"]) > 1:
				qmessage = kwargs["command"][1:]
			else:
				qmessage = "MeatBallBot is being shutdown."
				
			# Issues quit event
			self.push(IRCquit(qmessage), "issue-quit", self.channel)
			
		# Replies not bot admin
		else:
			self.push(IRCmessage(kwargs["destination"], "You are not a bot administrator."), "issue-message", self.channel)
	
	# Connects to network	
	@listener("command-connect")
	def connect(self, **kwargs):
		# Checks botadmin permissions
		if kwargs["botadmin"] >= 2:
		
			# Checks if netid was given
			if len(kwargs["command"]) > 1:
				netid = int(kwargs["command"][1])
				self.push(NewConnect(netid), "newconnect", self.channel)
		
			# Replies not enough information
			else:
				self.push(IRCmessage(kwargs["destination"], "More info plzkthnxbia."), "issue-message", self.channel)
			
		# Replies not bot admin
		else:
			self.push(IRCmessage(kwargs["destination"], "You are not a bot administrator."), "issue-message", self.channel)
			
	# Reconnects network
	@listener("command-reconnect")
	def reconnect(self, **kwargs):
		# Checks botadmin permissions
		if kwargs["botadmin"] >= 2:
		
			# Checks if a message is given
			if len(kwargs["command"]) > 1:
				qmessage = kwargs["command"][1:]
			else:
				qmessage = "MeatBallBot is being restarted."
				
			# Issues reconnect event
			self.push(Reconnect(message), "restart", self.channel)
			
		# Replies not bot admin
		else:
			self.push(IRCmessage(kwargs["destination"], "You are not a bot administrator."), "issue-message", self.channel)
		
	# Reconnects all network
	@listener("command-allreconnect")
	def allreconnect(self, **kwargs):
		# Checks botadmin permissions
		if kwargs["botadmin"] >= 2:
		
			# Checks if a message is given
			if len(kwargs["command"]) > 1:
				qmessage = kwargs["command"][1:]
			else:
				qmessage = "MeatBallBot is being restarted."
				
			# Issues reconnect all event
			self.push(AllReconnect(message), "allrestart", self.channel)
			
		# Replies not bot admin
		else:
			self.push(IRCmessage(kwargs["destination"], "You are not a bot administrator."), "issue-message", self.channel)
		
def startup(channel, cursor):
	plugin = Connections(channel = channel, cursor = cursor)
	return (__id__, plugin)

def kill():
	return __id__
